package com.example.geoaimavlink_android.activity.fragment

import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.SeekBar
import android.widget.SeekBar.OnSeekBarChangeListener
import com.example.geoaimavlink_android.R
import kotlinx.android.synthetic.main.frag_motor_test.btn_motor1
import kotlinx.android.synthetic.main.frag_motor_test.btn_motor2
import kotlinx.android.synthetic.main.frag_motor_test.btn_motor3
import kotlinx.android.synthetic.main.frag_motor_test.btn_motor4
import kotlinx.android.synthetic.main.frag_motor_test.seekbar
import kotlinx.android.synthetic.main.frag_motor_test.tv_progress

class MotorTestFragment: BaseFragment() {

    private var mCurrentProgress = 0

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return View.inflate(context, R.layout.frag_motor_test, null)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initView()
    }

    private fun initView() {
        seekbar.setOnSeekBarChangeListener(object : OnSeekBarChangeListener{
            override fun onProgressChanged(
                seekBar: SeekBar?,
                progress: Int,
                fromUser: Boolean
            ) {
                requireActivity().runOnUiThread { tv_progress?.text = "$progress %" }
                mCurrentProgress = progress
            }

            override fun onStartTrackingTouch(seekBar: SeekBar?) {
            }

            override fun onStopTrackingTouch(seekBar: SeekBar?) {
            }
        })
        btn_motor1.setOnClickListener {
            getProduct()?.flyControllerManager?.setMotorTestCommand(1, mCurrentProgress) {
                showToast(it?.description?:"success")
            }
        }
        btn_motor2.setOnClickListener {
            getProduct()?.flyControllerManager?.setMotorTestCommand(2, mCurrentProgress) {
                showToast(it?.description?:"success")
            }
        }
        btn_motor3.setOnClickListener {
            getProduct()?.flyControllerManager?.setMotorTestCommand(3, mCurrentProgress) {
                showToast(it?.description?:"success")
            }
        }
        btn_motor4.setOnClickListener {
            getProduct()?.flyControllerManager?.setMotorTestCommand(4, mCurrentProgress) {
                showToast(it?.description?:"success")
            }
        }
    }
}